#!/bin/sh
##### -*- mode:shell-script; indent-tabs-mode:nil; sh-basic-offset:2 -*-
##### Author: Travis Cross <tc@traviscross.com>

log1 () { printf '%s' "$1">&2; }
log () { printf '%s\n' "$1">&2; }
err () { log "$1"; exit 1; }

usage () {
  local opt="$1" bs="" be=""
  $opt && { bs="[ "; be=" ]"; }
  log "usage: $0 <corefile> ${bs}<path/to/freeswitch>${be}"
}

while getopts "h" o; do
  case "$o" in
    h) usage true; exit 0; ;;
  esac
done
shift $(($OPTIND-1))


if [ $# -lt 1 ]; then
  usage true; exit 1
fi
core="$1"
if ! [ $# -lt 2 ]; then
  fspath="$2"
  [ -x "$fspath" ] || err "Not executable: $fspath"
fi
btpath="/tmp/$(date -u +%Y%m%dT%H%M%SZ)-bt.txt"
if [ -z "$fspath" ]; then
  for x in "$(which freeswitch)" \
    /usr/bin/freeswitch /usr/sbin/freeswitch \
    /usr/local/bin/freeswitch /usr/local/sbin/freeswitch \
    /opt/freeswitch/bin/freeswitch; do
    ! [ -x "$x" ] || { fspath="$x"; break; }
  done
fi
if [ -z "$fspath" ]; then
  log "Couldn't find FS binary"
  usage false; exit 1
fi
if test $(id -u) = 0 && test -f /etc/debian_version; then
  cat >&2 <<'EOF'
### You're running on Debian.  Please make sure you have appropriate
### freeswitch-*-dbg packages installed so we get as many symbols in
### this backtrace as possible.  I won't install these for you.  If
### you're running the freeswitch-all package, then you should install
### freeswitch-all-dbg.
EOF
log ''
fi

log1 'Generating backtrace...'
gdb "$fspath" "$core" > $btpath <<'EOF'
set prompt
set pagination off
printf "\n\n"
printf "================================================================================\n"
printf "# GDB session generated by FS backtrace-from-core\n"
printf "# FreeSWITCH version: %s\n", switch_version_full_str
printf "# FreeSWITCH version (human): %s\n", switch_version_full_human_str
printf "================================================================================\n"
printf "\n\n"
printf "================================================================================\n"
printf "# info threads\n"
printf "================================================================================\n"
info threads
printf "================================================================================\n"
printf "# bt\n"
printf "================================================================================\n"
bt
printf "================================================================================\n"
printf "# bt full\n"
printf "================================================================================\n"
bt full
printf "================================================================================\n"
printf "# thread apply all bt\n"
printf "================================================================================\n"
thread apply all bt
printf "================================================================================\n"
printf "# thread apply all bt full\n"
printf "================================================================================\n"
thread apply all bt full
quit
EOF
log 'done'
log ''
log "Please attach the backtrace here:"
log "$btpath"
